
var TINY={};

function T$(i){return document.getElementById(i)}
function T$$(e,p){return p.getElementsByTagName(e)}

TINY.accordion=function(){
	function slider(n)
	{
		this.n = n;
		this.a = [];
	}
	slider.prototype.init=function(t,e,m,o,k)
	{
		var a=T$(t), i=s=0, n=a.childNodes, l=n.length; this.s=k||0; this.m=m||0;
		for(i; i < l; i++){
			var v=n[i];
			if(v.nodeType!=3)
			{
				this.a[s] = {};
				this.a[s].h = h = T$$(e,v)[0];
				this.a[s].c = c = T$$('div',v)[0];
				h.onclick = new Function(this.n + '.pr(0,' + s + ')');
				if(o == s)
				{
					h.className = this.s;
					c.style.height = 'auto';
					c.d = 1;
				}
				else
				{
					c.style.height = 0;
					c.d = -1;
				} 
				s++;
			}
		}
		this.l = s;
	};
	slider.prototype.pr=function(f,d){
		for(var i=0;i<this.l;i++){
			var h=this.a[i].h, c=this.a[i].c, k=c.style.height; k=k=='auto'?1:parseInt(k); clearInterval(c.t);
			if((k!=1&&c.d==-1)&&(f==1||i==d)){
				c.style.height=''; c.m=c.offsetHeight; c.style.height=k+'px'; c.d=1; h.className=this.s; su(c,1)
			}else if(k>0&&(f==-1||this.m||i==d)){
				c.d=-1; h.className=''; su(c,-1)
			}
		}
	};
	function su(c){c.t=setInterval(function(){sl(c)},20)};
	function sl(c){
		var h=c.offsetHeight, d=c.d==1?c.m-h:h; c.style.height=h+(Math.ceil(d/5)*c.d)+'px';
		c.style.opacity=h/c.m; c.style.filter='alpha(opacity='+h*100/c.m+')';
		if((c.d==1&&h>=c.m)||(c.d!=1&&h==1)){if(c.d==1){c.style.height='auto'} clearInterval(c.t)}
	};
	return{slider:slider}
}();



function next()
{
	offset = offset + 5;
	requestHomeworkToDo();
}

function previous()
{
	offset = offset - 5;
	requestHomeworkToDo();
}


function close_info()
{
	document.getElementById("info").style.display = "none";
}

//Validates the form that contains the homework
function check_form(number)
{
	var form = document.getElementById("homework_" + number);
	var submit = true;
	if (form.answer.value == "")
	{
		document.getElementById("answer_entered").innerHTML = " *";
		submit = false;
	}
	
	if(submit == true)
	{
		var result = eval('(' + service.sendAnswer(form.id.value, form.teacher.value, form.answer.value) + ')');
		if(result.result == "ok")
		{
			document.getElementById("infomessage").innerHTML = "The answer has been sent to " + form.teacher.value + ". You will receive a notification as soon as he/she corrects and marks the exercise."
			document.getElementById("info").style.display = "block";
			var a = document.getElementById("questions_answer");
			var value = a.innerHTML;
			var number = value.slice(1, value.length - 1);
			var newNumber = parseInt(number) - 1;
			a.innerHTML = " (" + newNumber + ")";
			offset = 0;
			requestHomeworkToDo();
		}
		else if(result.result == "error")
		{
			document.getElementById("infomessage").innerHTML = "An error has occurred. Please, try to send the answer again."
			document.getElementById("info").style.display = "block";
		}
	}
}

//Obtains the list of users that study a language
function requestHomeworkToDo() {
	try {
		var homeworks = eval('(' + service.requestHomeworkToDo(offset) + ')');
		var i = 0;
		var number = 0;
		var list = document.getElementById("homework_list");
		list.innerHTML = "";
		for (var homework in homeworks)
		{
			if(i == 0)
			{
				number = homeworks[i];
				if(number == 0)
				{
					document.getElementById("texto").innerHTML = "You do not have any homework to do.";
				}
				i = i + 1;
			}
			else if(homeworks[i] != undefined)
			{
				var hour = homeworks[i].hour;
				var minute = homeworks[i].minute;
				if(hour < 10) hour = "0" + hour;
				if(minute < 10) minute = "0" + minute;
				list.innerHTML += 
				"<li>" +
					"<h3>" +
						"<a class=\"user\">Teacher: " + homeworks[i].teacher + ", Difficulty: " + homeworks[i].difficulty + "</a>" +
						"<a class=\"date\">created at " + hour + ":" + minute + ",  " + 
						homeworks[i].day + "-" + homeworks[i].month + "-" + homeworks[i].year + "</a>" +
							"</h3>" +
							"<div class=\"homework\">" +
								"<div class=\"homework_content\">" +
									"<form name=\"homework_" + i + "\" id=\"homework_" + i + "\">" +
										"<label>Question:</label>" +
											"<input type=\"hidden\" name=\"id\" value=\"" + homeworks[i].id + "\"id\"/>" +
												"<input type=\"hidden\" name=\"teacher\" value=\"" + homeworks[i].teacher + "\"/>" +
												"<textarea name=\"question\" class=\"question\" readonly=\"readonly\" rows=\"4\">" + 
												homeworks[i].question + "</textarea>" +
												"<label>Answer:</label>" +
												"<a id=\"answer_entered\" style=\"margin-right:485px;color:red\"></a>" +
												"<textarea name=\"answer\" class=\"answer\" rows=\"6\"></textarea>" +
												"<a class=\"submit\" href=\"#title\" onclick=\"javascript:check_form(" + i + ");\">Send answer</a>" +
											"</form>" +
										"</div>" +
									"</div>" +
								"</li>";
				i = i + 1;
			}
		}
		if(offset > 0 || number > offset + 5)
		{
			var next = "";
			var previous = "";
			if(offset > 0)
			{
				previous = "<a class=\"previous\" href=\"javascript:previous();\"><strong>Previous</strong></a>";
			}
			if(number > offset + 5)
			{
				next = "<a class=\"next\" href=\"javascript:next();\"><strong>Next</strong></a>";
			}
			document.getElementById("next_previous").innerHTML = previous + next;
		}
	} catch(e) {
		alert(e);
	}
	parentAccordion.init("homework_list","h3",0,0);
	return false;
}
